package com.aplink.generic;

import java.util.ArrayList;

import android.app.ActionBar;
import android.app.Activity;
import android.app.ProgressDialog;
import android.app.SearchManager;
import android.app.SearchableInfo;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.util.SparseArray;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.widget.SearchView;
import android.widget.SearchView.OnCloseListener;
import android.widget.TextView;

import com.aplink.generic.adapter.ActionbarSelectionAdapter;
import com.aplink.generic.bitmap.ImageCache;
import com.aplink.generic.bitmap.ImageFetcher;
import com.aplink.generic.cache.ContentDataManager;
import com.aplink.generic.cache.FormDesignManager;
import com.aplink.generic.constant.BundleConstant;
import com.aplink.generic.constant.type.ActionType;
import com.aplink.generic.constant.type.ControlType;
import com.aplink.generic.constant.type.DialogStatus;
import com.aplink.generic.database.DatabaseHelper;
import com.aplink.generic.database.SqlMethods;
import com.aplink.generic.dialog.ApLinkProgressDialog;
import com.aplink.generic.dialog.SparseDialog;
import com.aplink.generic.dictionary.GenericDictionary;
import com.aplink.generic.handler.DataHandler;
import com.aplink.generic.handler.EventHandler;
import com.aplink.generic.handler.ResponseHandler;
import com.aplink.generic.handler.WebserviceHandler;
import com.aplink.generic.interfaces.DatabaseDelete;
import com.aplink.generic.interfaces.DatabaseGet;
import com.aplink.generic.interfaces.DatabaseInsert;
import com.aplink.generic.interfaces.DatabaseUpdate;
import com.aplink.generic.interfaces.FinishActivityListener;
import com.aplink.generic.interfaces.FragmentStateListener;
import com.aplink.generic.interfaces.OnActionBarSelectionListener;
import com.aplink.generic.interfaces.OnActivityResultListener;
import com.aplink.generic.interfaces.OnBackPressedListerner;
import com.aplink.generic.interfaces.OnControlSubmitListener;
import com.aplink.generic.interfaces.OnRotateListener;
import com.aplink.generic.interfaces.OnSearchListener;
import com.aplink.generic.interfaces.StartActivityListener;
import com.aplink.generic.pojo.ContentData;
import com.aplink.generic.pojo.GenericAttribute;
import com.aplink.generic.pojo.GenericObject;
import com.aplink.generic.pojo.ObjPropInfo;
import com.aplink.generic.pojo.controlspec.ButtonSpec;
import com.aplink.generic.pojo.controlspec.ControlSpec;
import com.aplink.generic.pojo.controlspec.GallerySpec;
import com.aplink.generic.pojo.controlspec.ListviewSpec;
import com.aplink.generic.pojo.controlspec.actionbar.ActionBarSpec;
import com.aplink.generic.pojo.controlspec.actionbar.SearchViewSpec;
import com.aplink.generic.pojo.controlspec.actionbar.SelectionSpec;
import com.aplink.generic.pojo.controlspec.gridview.GridViewSpec;
import com.aplink.generic.pojo.controlspec.menu.MenuItemSpec;
import com.aplink.generic.pojo.formdesign.Column;
import com.aplink.generic.pojo.formdesign.FormDesign;
import com.aplink.generic.pojo.formdesign.InitValue;
import com.aplink.generic.pojo.formdesign.PassingValue;
import com.aplink.generic.pojo.formdesign.SelectWhere;
import com.aplink.generic.sharepreference.GenericSharePreference;
import com.aplink.generic.util.Language;
import com.aplink.generic.util.LogUtil;
import com.aplink.generic.util.ResourcesHelper;
import com.aplink.generic.util.objectrule.EnableCondition;
import com.aplink.generic.util.objectrule.Target;
import com.aplink.generic.util.objectrule.TargetCollection;
import com.aplink.generic.util.validation.ControlValidation;
import com.aplink.generic.util.validation.ControlValidationTask;
import com.aplink.generic.util.validation.ValidationResult;
import com.aplink.generic.widgets.ApLinkAutoCompleteTextView;
import com.aplink.generic.widgets.ApLinkBaseView;
import com.aplink.generic.widgets.ApLinkButton;
import com.aplink.generic.widgets.ApLinkCheckBox;
import com.aplink.generic.widgets.ApLinkDateTimePicker;
import com.aplink.generic.widgets.ApLinkEditText;
import com.aplink.generic.widgets.ApLinkFloatingAutoComplete;
import com.aplink.generic.widgets.ApLinkFloatingEditText;
import com.aplink.generic.widgets.ApLinkGallery;
import com.aplink.generic.widgets.ApLinkGoogleMaps;
import com.aplink.generic.widgets.ApLinkGridView;
import com.aplink.generic.widgets.ApLinkImageView;
import com.aplink.generic.widgets.ApLinkListView;
import com.aplink.generic.widgets.ApLinkRadioButton;
import com.aplink.generic.widgets.ApLinkSpinner;
import com.aplink.generic.widgets.ApLinkSwitch;
import com.aplink.generic.widgets.ApLinkTabhost;
import com.aplink.generic.widgets.ApLinkTextview;
import com.aplink.generic.widgets.ApLinkToggleButton;
import com.aplink.generic.widgets.ApLinkViewPager;
import com.aplink.sg.R;

public class GenericFragment extends Fragment implements OnClickListener,
		ActionBar.OnNavigationListener, OnBackPressedListerner,
		AnimationListener {
	public static final String CONTENT_DATA = "CONTENT_DATA";
	public static final String FORM_DESIGN = "FORM_DESIGN";
	private static final String IMAGE_CACHE_DIR = "images";
	public static final int NONE = 0;
	private static final int REFRESH_DATA = 1234;
	
	public static final GenericFragment newInstance(final int formDesignID,
			final int contentDataID) {
		final GenericFragment fragment = new GenericFragment();
		final Bundle bundle = new Bundle();
		bundle.putInt(GenericFragment.FORM_DESIGN, formDesignID);
		bundle.putInt(GenericFragment.CONTENT_DATA, contentDataID);
		fragment.setArguments(bundle);
		return fragment;
	}
	
	private boolean isAlowRefresh;
	private ActionbarSelectionAdapter mActionbarSelectionAdapter;
	private OnActionBarSelectionListener mActionBarSelectionListener;
	private OnActivityResultListener mActivityResultHandler;
	private int mAlowRefresh;
	private SparseDialog mApLinkAlertDialog;
	private ApLinkProgressDialog mApLinkProgressDialog;
	private GenericDictionary<String, ApLinkBaseView> mBaseViewByID;
	private GenericDictionary<String, ApLinkBaseView> mBaseViewByMappingName;
	private ContentData mContentData;
	private EventHandler mControlEventHandler;
	private DataHandler mDatabaseHandler;
	private FormDesign mFormDesign;
	private FragmentStateListener mFragmentStateListener;
	private ResponseHandler mGenericHandler;
	private int mIgnoreSelectionIndex;
	private GenericObject mInitValues;
	private ArrayList<ApLinkBaseView> mListRefreshView;
	private OnControlSubmitListener mOnControlSubmit;
	private OnRotateListener mOnRotateListener;
	private OnSearchListener mOnSearchListener;
	private GenericObject mPassedGenericObject;
	private PassingValue mPassingValue;
	private SearchView mSearchView = null;
	private SearchViewSpec mSearchViewSpec;
	private ControlSpec mSelectedControlSpec;
	
	private GenericObject mSelectedObject = null;
	
	private SqlMethods mSqlMethods = null;
	
	private Target mTargetClass = null;
	
	private GenericDictionary<String, ArrayList<ControlSpec>> mValidationGroups;
	
	private ArrayList<ApLinkBaseView> mValueListViews;
	
	private View mView;
	
	private WebserviceHandler mWebserviceHandler;
	
	private OnClickListener onActionbarButtonClickListener = new OnClickListener() {
		
		@Override
		public void onClick(final View v) {
			onSubmit((MenuItemSpec) v.getTag());
		}
	};
	
	public GenericFragment() {
	}
	
	public void clearAll() {
		mPassedGenericObject.clear();
		initData();
		mappingControl(null);
	}
	
	public View findViewByID(final String id) {
		final int viewID = ResourcesHelper.getViewIdByName(id);
		if (viewID != ResourcesHelper.RESOURCE_NOT_FOUND) {
			return getView().findViewById(viewID);
		}
		return null;
	}
	
	public FinishActivityListener finish() {
		return new FinishActivityListener() {
			@Override
			public void finishActivity() {
				GenericFragment.this.getActivity().setResult(
						GenericFragment.this.mAlowRefresh);
				GenericFragment.this.getActivity().finish();
				getActivity().overridePendingTransition(
						getFormDesign().getAnimationInID(),
						getFormDesign().getAnimationOutID());
			}
		};
	}
	
	public void finishActivity() {
		finish().finishActivity();
	}
	
	public OnActionBarSelectionListener getActionBarSelectionListener() {
		return this.mActionBarSelectionListener;
	}
	
	public OnActivityResultListener getActivityResultListener() {
		return this.mActivityResultHandler;
	}
	
	public SparseDialog getAlertDialog() {
		return this.mApLinkAlertDialog;
	}
	
	public ApLinkProgressDialog getApLinkProgressDialog() {
		return this.mApLinkProgressDialog;
	}
	
	public ApLinkBaseView getAPLinkViewByID(final String id) {
		return this.mBaseViewByID.get(id);
	}
	
	public Context getApplicationContext() {
		return getActivity().getBaseContext();
	}
	
	public final ContentData getContentData() {
		return mContentData;
	}
	
	public DatabaseDelete getDatabaseDelete() {
		return DatabaseHelper.getInstance();
	}
	
	public DatabaseGet getDatabaseGet() {
		return DatabaseHelper.getInstance();
	}
	
	public DataHandler getDatabaseHandler() {
		return this.mDatabaseHandler;
	}
	
	public DatabaseInsert getDatabaseInsert() {
		return DatabaseHelper.getInstance();
	}
	
	public DatabaseUpdate getDatabaseUpdate() {
		return DatabaseHelper.getInstance();
	}
	
	public EventHandler getEventHandler() {
		return this.mControlEventHandler;
	}
	
	public final FormDesign getFormDesign() {
		return mFormDesign;
	}
	
	public FragmentStateListener getFragmentStateListener() {
		return mFragmentStateListener;
	}
	
	public String getFullClassName() {
		return ((Object) this).getClass().getName();
	}
	
	public GenericObject getInitValues() {
		return this.mInitValues;
	}
	
	public final GenericObject getMappingObject() {
		handleInitValue();
		return this.getMappingObject(false);
	}
	
	public final GenericObject getMappingObject(
			final boolean isForceRenewCreatedTime) {
		final GenericObject objValues = new GenericObject();
		final String createdTime = GenericApplication.getInstance()
				.getCreatedTime();
		final ContentData objPropCol = getContentData();
		if (objPropCol == null) {
			return objValues;
		}
		objValues.setTableName(objPropCol.getMainTable());
		for (int i = 0; i < objPropCol.size(); i++) {
			final ObjPropInfo propInfo = objPropCol.valueAt(i);
			final ApLinkBaseView baseView = this.mBaseViewByMappingName
					.get(propInfo.getColumnName());
			GenericAttribute attribute = null;
			if (baseView != null) {
				attribute = baseView.getAttribute();
			} else {
				GenericAttribute passedAttribute = null;
				if (mInitValues.isContainValueOf(propInfo)) {
					passedAttribute = this.mInitValues.get(propInfo
							.getFullColumnName());
				} else if (mPassedGenericObject != null
						&& mPassedGenericObject.isContainValueOf(propInfo)) {
					if (propInfo.isCreatedTime()) {
						if (!isForceRenewCreatedTime) {
							passedAttribute = this.mPassedGenericObject
									.get(propInfo.getFullColumnName());
						}
					} else {
						passedAttribute = this.mPassedGenericObject
								.get(propInfo.getFullColumnName());
					}
				}
				
				if ((passedAttribute != null) && passedAttribute.isValid()) {
					attribute = passedAttribute;
				} else if (propInfo.isCreatedTime()
						|| (propInfo.isKey() & !propInfo.isAuto())) {
					attribute = GenericAttribute.obtain(createdTime, propInfo);
				} else {
					attribute = GenericAttribute.obtain(null, propInfo);
				}
			}
			objValues.put(propInfo.getFullColumnName(), attribute);
		}
		for (final ApLinkBaseView baseView : this.mValueListViews) {
			GenericObject genericObject = new GenericObject(
					baseView.getListAttributes());
			objValues.merge(genericObject);
		}
		return objValues;
	}
	
	public OnClickListener getOnActionbarButtonClickListener() {
		return this.onActionbarButtonClickListener;
	}
	
	public OnRotateListener getOnRotateListener() {
		return mOnRotateListener;
	}
	
	public OnSearchListener getOnSearchListener() {
		return this.mOnSearchListener;
	}
	
	public GenericObject getPassedObject() {
		if (mPassedGenericObject == null) {
			return new GenericObject();
		}
		return this.mPassedGenericObject;
	}
	
	private GenericObject getPassedObject(Bundle bundle) {
		GenericObject passedValues = new GenericObject();
		if (bundle != null) {
			if (bundle.containsKey(BundleConstant.SELECTED_OBJECT)) {
				SparseArray<GenericAttribute> sparseArray = bundle
						.getSparseParcelableArray(BundleConstant.SELECTED_OBJECT);
				for (int i = 0; i < sparseArray.size(); i++) {
					passedValues.put(sparseArray.keyAt(i),
							sparseArray.valueAt(i));
				}
			}
		}
		return passedValues;
	}
	
	public ProgressDialog getProgressDialog(final ActionType actionType) {
		if (this.mApLinkProgressDialog != null) {
			return this.mApLinkProgressDialog.getProgressDialog(actionType
					.getValue());
		}
		return null;
	}
	
	public ResponseHandler getResponseHandler() {
		if (this.mGenericHandler == null) {
			this.mGenericHandler = new ResponseHandler(this);
		}
		return this.mGenericHandler;
	}
	
	public SearchView getSearchView() {
		return mSearchView;
	}
	
	private SearchViewSpec getSearchViewSpec() {
		if (mSearchViewSpec == null && getFormDesign().hasActionBar()) {
			mSearchViewSpec = getFormDesign().getActionBar()
					.getSearchViewSpec();
		}
		return mSearchViewSpec;
	}
	
	public ControlSpec getSelectedControlSpec() {
		return mSelectedControlSpec;
	}
	
	public GenericObject getSelectedObject() {
		return this.mSelectedObject;
	}
	
	public SqlMethods getSqlMethods() {
		return this.mSqlMethods;
	}
	
	public SqlMethods getSqlMethods(final GenericObject object,
			final ArrayList<Column> conditions) {
		if (this.mFormDesign != null) {
			final SqlMethods sqlMethods = SqlMethods.getInstance();
			for (int i = 0; i < conditions.size(); i++) {
				final Column column = conditions.get(i);
				final String cond = column.getRealColumnName();
				if (column.getDefaultValue() != null) {
					final String value = column.getDefaultValue();
					sqlMethods.equal(cond, value);
					if (i < (conditions.size() - 1)) {
						sqlMethods.and();
					}
				} else {
					final GenericAttribute obj = object.get(cond);
					if ((obj != null) && !TextUtils.isEmpty(obj.getValue())) {
						// final ObjPropInfo objPropInfo = getContentData().get(
						// cond);
						final String value = String.valueOf(obj.getValue());
						// if (objPropInfo != null) {
						// sqlMethods.equal(objPropInfo, value);
						// } else {
						// sqlMethods.equal(cond, value);
						// }
						sqlMethods.equal(cond, value);
						if (i < (conditions.size() - 1)) {
							sqlMethods.and();
						}
					}
				}
			}
			return sqlMethods;
		}
		return null;
	}
	
	protected SqlMethods getSqlMethods(final GenericObject object,
			final String[] conditions) {
		if (this.mFormDesign != null) {
			final SqlMethods sqlMethods = SqlMethods.getInstance();
			for (int i = 0; i < conditions.length; i++) {
				final String cond = conditions[i];
				final GenericAttribute obj = object.get(cond);
				final ObjPropInfo objPropInfo = getContentData().get(cond);
				final String value = String.valueOf(obj.getValue());
				sqlMethods.equal(objPropInfo, value);
				if (i < (conditions.length - 1)) {
					sqlMethods.and();
				}
			}
			return sqlMethods;
		}
		return null;
	}
	
	public Target getTargetClass() {
		return this.mTargetClass;
	}
	
	private void getTargetClassToNavigate(ControlSpec controlSpec) {
		try {
			TargetCollection targetCollection = null;
			if (controlSpec instanceof MenuItemSpec) {
				targetCollection = ((MenuItemSpec) controlSpec)
						.getTargetCollection();
			} else if (controlSpec instanceof ButtonSpec) {
				targetCollection = ((ButtonSpec) controlSpec)
						.getTargetCollection();
			}
			if (targetCollection != null) {
				setTargetClass(targetCollection
						.getTargetClass(this.mSelectedObject));
			}
		} catch (final NameNotFoundException | ClassNotFoundException e) {
			LogUtil.error(e.getMessage(), "onClick", getClass());
		}
	}
	
	public GenericDictionary<String, ArrayList<ControlSpec>> getValidationGroups() {
		return this.mValidationGroups;
	}
	
	public ApLinkBaseView getViewByMappingName(final String mappingName) {
		return this.mBaseViewByMappingName.get(mappingName);
	}
	
	public GenericDictionary<String, ApLinkBaseView> getViewsInLayout() {
		return this.mBaseViewByMappingName;
	}
	
	public WebserviceHandler getWebserviceHandler() {
		return this.mWebserviceHandler;
	}
	
	private void handleActionBar() {
		final ActionBar actionBar = getActivity().getActionBar();
		SelectionSpec selectionSpec = null;
		if (getFormDesign().getActionBar() != null) {
			selectionSpec = getFormDesign().getActionBar().getSelectionSpec();
		}
		if (actionBar != null) {
			final ActionBarSpec actionBarSpec = getFormDesign().getActionBar();
			if (actionBarSpec != null) {
				actionBar.setDisplayHomeAsUpEnabled(actionBarSpec
						.isDisplayHomeAsUp());
				actionBar.setDisplayShowHomeEnabled(actionBarSpec
						.isDisplayShowHomeEnabled());
				actionBar.setDisplayShowTitleEnabled(actionBarSpec
						.isDisplayShowTitleEnabled());
				if (!TextUtils.isEmpty(actionBarSpec.getTitle())) {
					actionBar.setTitle(Language.getInstance().get(
							actionBarSpec.getTitle()));
				}
				if (!TextUtils.isEmpty(actionBarSpec.getSubTitle())) {
					actionBar.setSubtitle(Language.getInstance().get(
							actionBarSpec.getSubTitle()));
				}
				if (actionBarSpec.getIconID() != ResourcesHelper.RESOURCE_NOT_FOUND) {
					actionBar.setIcon(actionBarSpec.getIconID());
				}
				if (GenericApplication.getInstance().getMainMenu() != null) {
					actionBar.setDisplayHomeAsUpEnabled(true);
					actionBar.setDisplayShowHomeEnabled(true);
					actionBar.setHomeButtonEnabled(true);
				}
				if (!actionBarSpec.getActionBarButtons().isEmpty()) {
					actionBar.setDisplayOptions(ActionBar.DISPLAY_HOME_AS_UP
							| ActionBar.DISPLAY_SHOW_CUSTOM);
					actionBar.setCustomView(actionBarSpec
							.getActionBarButton(this));
				} else if (selectionSpec != null) {
					this.mIgnoreSelectionIndex = selectionSpec.getIgnoreIndex();
					actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
					this.mActionbarSelectionAdapter = new ActionbarSelectionAdapter(
							getApplicationContext(), selectionSpec);
					actionBar.setListNavigationCallbacks(
							this.mActionbarSelectionAdapter, this);
				} else {
					actionBar.setNavigationMode(ActionBar.DISPLAY_SHOW_TITLE);
				}
			}
		}
	}
	
	private void handleInitValue() {
		if (!this.mFormDesign.getControlSpecCollection().isEmpty()) {
			final InitValue initValue = this.mFormDesign.getInitValue();
			this.mInitValues = new GenericObject();
			final Intent intent = getActivity().getIntent();
			final Bundle bundle = intent.getExtras();
			final GenericObject passedValues = new GenericObject();
			if ((bundle != null)
					&& bundle.containsKey(BundleConstant.SELECTED_OBJECT)) {
				final SparseArray<GenericAttribute> sparseArray = bundle
						.getSparseParcelableArray(BundleConstant.SELECTED_OBJECT);
				for (int i = 0; i < sparseArray.size(); i++) {
					passedValues.put(sparseArray.keyAt(i),
							sparseArray.valueAt(i));
					if (sparseArray.valueAt(i) != null) {
						mInitValues.put(sparseArray.keyAt(i),
								sparseArray.valueAt(i));
					}
				}
			}
			for (final Column column : initValue) {
				if (column.getPreferenceType() != null) {
					final String value = GenericSharePreference.getInstance()
							.getValue(column.getPreferenceType());
					this.mInitValues.put(column.getRealColumnName(), value);
				} else if (column.getDefaultValue() != null) {
					this.mInitValues.put(column.getRealColumnName(),
							column.getDefaultValue());
				} else if (passedValues != null) {
					this.mInitValues.put(column.getRealColumnName(),
							passedValues.get(column.getColumnName()));
				}
			}
		}
	}
	
	private boolean hasLoadedData;
	
	private void handleSelectWhere() {
		final Intent intent = getActivity().getIntent();
		if (!this.mFormDesign.getControlSpecCollection().isEmpty()) {
			final SelectWhere selectWhere = this.mFormDesign.getSelectWhere();
			if (!selectWhere.isEmpty()) {
				final GenericObject passedValues = getPassedObject(intent
						.getExtras());
				final GenericObject passedObjects = new GenericObject();
				for (final Column column : selectWhere) {
					String value = null;
					if (passedValues.containsKey(column.getColumnName())) {
						value = passedValues.get(column.getColumnName())
								.getValue();
					}
					if (TextUtils.isEmpty(value)) {
						value = column.getDefaultValue();
					}
					if (!TextUtils.isEmpty(value)) {
						passedObjects.put(column.getRealColumnName(), value);
					}
				}
				this.mSqlMethods = this.getSqlMethods(new GenericObject(
						passedObjects), selectWhere);
				if (mContentData == null || this.mContentData.isEmpty()) {
					mPassedGenericObject = passedObjects;
				} else {
					if (!TextUtils.isEmpty(mSqlMethods.toString())) {
						final GenericObject genericObject = getDatabaseGet()
								.get(getContentData(), mSqlMethods.toString());
						setHasLoadedData(true);
						if (genericObject.isEmpty()) {
							mPassedGenericObject = passedObjects;
						} else {
							mPassedGenericObject = genericObject;
						}
					}
				}
			} else if (this.mFormDesign.isAlwayLoadFirstRecord()) {
				final GenericObject genericObject = getDatabaseGet().get(
						getContentData(), null);
				mPassedGenericObject = genericObject;
			}
		}
	}
	
	public void handleValidationResult(ControlSpec controlSpec,
			final ValidationResult result) {
		final ActionType actionType = controlSpec.getActionType();
		if (result.isValid()) {
			sendActionToEventHandler(controlSpec, actionType);
		} else {
			if (actionType != null) {
				if (hasRegisterProgressDialog(actionType)) {
					getProgressDialog(actionType).dismiss();
				}
				showInvalidValidationDialog(actionType, result);
			}
		}
	}
	
	public boolean hasActionBarSelectionFor(final String controlID) {
		if (getFormDesign().getActionBar() != null) {
			final SelectionSpec selectionSpec = getFormDesign().getActionBar()
					.getSelectionSpec();
			if (selectionSpec != null) {
				return selectionSpec.getSearchFor().equalsIgnoreCase(controlID);
			}
		}
		return false;
	}
	
	public boolean hasRegisterAlertDialog(final ActionType actionType,
			final DialogStatus dialogStatus) {
		return getAlertDialog().has(actionType, dialogStatus);
	}
	
	public boolean hasRegisterProgressDialog(final ActionType actionType) {
		if (this.mApLinkProgressDialog != null) {
			return this.mApLinkProgressDialog.has(actionType);
		}
		return false;
	}
	
	protected void initData() {
		if ((this.mPassedGenericObject == null)
				|| this.mPassedGenericObject.isEmpty()) {
			handleSelectWhere();
		}
		handleInitValue();
		if (mInitValues != null && !mInitValues.isEmpty()) {
			if (mPassedGenericObject == null) {
				mPassedGenericObject = new GenericObject();
			}
			mPassedGenericObject.merge(mInitValues);
		}
	}
	
	public void invalidateOptionsMenu() {
		if (getActivity() != null) {
			getActivity().invalidateOptionsMenu();
		}
	}
	
	public boolean isAlowRefresh() {
		return this.isAlowRefresh;
	}
	
	public ValidationResult isFormValidate(final ControlSpec controlSpec) {
		final String validationGroup = controlSpec.getValidationGroup();
		if (validationGroup != null) {
			return ControlValidation.getInstance().isFormValidate(this,
					validationGroup);
		}
		final ValidationResult result = new ValidationResult();
		result.setValid(true);
		return result;
	}
	
	public void languageChanged() {
		if (((GenericActivity) getActivity()).getDrawerAdapter() != null) {
			((GenericActivity) getActivity()).getDrawerAdapter()
					.notifyDataSetChanged();
		}
		if (getParentFragment() != null) {
			((GenericFragment) getParentFragment()).languageChanged();
		}
		for (int i = 0; i < mBaseViewByID.size(); i++) {
			mBaseViewByID.valueAt(i).languageChanged();
		}
		translate(mView, true);
		final ActionBar actionBar = getActivity().getActionBar();
		if (actionBar != null) {
			final ActionBarSpec actionBarSpec = getFormDesign().getActionBar();
			if (actionBarSpec != null) {
				if (!TextUtils.isEmpty(actionBarSpec.getTitle())) {
					actionBar.setTitle(Language.getInstance().get(
							actionBarSpec.getTitle()));
				}
				if (!TextUtils.isEmpty(actionBarSpec.getSubTitle())) {
					actionBar.setSubtitle(Language.getInstance().get(
							actionBarSpec.getSubTitle()));
				}
			}
		}
	}
	
	private GenericDictionary<Integer, ApLinkBaseView> mNotTranslate;
	
	protected void mappingControl(Bundle bundle) {
		for (int i = 0; i < getFormDesign().getControlSpecCollection().size(); i++) {
			final ControlSpec controlSpec = getFormDesign()
					.getControlSpecCollection().valueAt(i);
			final ControlType controlType = controlSpec.getControlType();
			int controlId = ResourcesHelper.RESOURCE_NOT_FOUND;
			if (controlType.equals(ControlType.TABHOST)) {
				controlId = android.R.id.tabhost;
			} else if (controlType.equals(ControlType.LISTVIEW)
					&& controlSpec.getIDName().equalsIgnoreCase(
							"android.R.id.list")) {
				controlId = android.R.id.list;
			} else {
				controlId = ResourcesHelper.getViewIdByName(controlSpec
						.getIDName());
			}
			if ((controlId == ResourcesHelper.RESOURCE_NOT_FOUND)
					&& (!controlType.equals(ControlType.DATETIME_PICKER))
					&& (!controlType.equals(ControlType.GOOGLE_MAPS))) {
				continue;
			}
			View view = null;
			if (controlId == mView.getId()) {
				view = mView;
			} else {
				view = this.mView.findViewById(controlId);
			}
			if (view != null) {
				view.setTag(R.id.CONTROL_SPEC, controlSpec);
			}
			ApLinkBaseView baseView = null;
			if (controlType.equals(ControlType.DATETIME_PICKER)) {
				baseView = new ApLinkDateTimePicker(this, controlSpec, view,
						bundle);
			} else if (controlType.equals(ControlType.BUTTON)) {
				
				baseView = new ApLinkButton(this, controlSpec, view, bundle);
				
			} else if (controlType.equals(ControlType.TEXTVIEW)) {
				baseView = new ApLinkTextview(this, controlSpec, view, bundle);
			} else if (controlType.equals(ControlType.LISTVIEW)) {
				baseView = new ApLinkListView(this, controlSpec, view, bundle);
				if (getSearchViewSpec() != null) {
					final OnSearchListener onSearchListener = ((ApLinkListView) baseView)
							.getOnSearchListener();
					setOnSearchListener(onSearchListener);
				}
				if (((ListviewSpec) controlSpec).isAutoRefresh()) {
					this.mListRefreshView.add(baseView);
				}
			} else if (controlType.equals(ControlType.GRIDVIEW)) {
				baseView = new ApLinkGridView(this, controlSpec, view, bundle);
				if (((GridViewSpec) controlSpec).isAutoRefresh()) {
					this.mListRefreshView.add(baseView);
				}
			} else if (controlType.equals(ControlType.EDITTEXT)) {
				baseView = new ApLinkEditText(this, controlSpec, view, bundle);
			} else if (controlType.equals(ControlType.FLOATTING_EDITTEXT)) {
				baseView = new ApLinkFloatingEditText(this, controlSpec, view,
						bundle);
			} else if (controlType.equals(ControlType.SPINNER)) {
				baseView = new ApLinkSpinner(this, controlSpec, view, bundle);
			} else if (controlType.equals(ControlType.VIEWPAGER)) {
				baseView = new ApLinkViewPager(this, controlSpec, view, bundle);
			} else if (controlType.equals(ControlType.TABHOST)) {
				baseView = new ApLinkTabhost(this, controlSpec, view, bundle);
			} else if (controlType.equals(ControlType.IMAGEVIEW)) {
				baseView = new ApLinkImageView(this, controlSpec, view, bundle);
			} else if (controlType.equals(ControlType.RADIO_BUTTON)) {
				baseView = new ApLinkRadioButton(this, controlSpec, view,
						bundle);
			} else if (controlType.equals(ControlType.CHECKBOX)) {
				baseView = new ApLinkCheckBox(this, controlSpec, view, bundle);
			} else if (controlType.equals(ControlType.GALLERY)) {
				baseView = new ApLinkGallery(this, controlSpec, view, bundle);
				final GallerySpec gallerySpec = (GallerySpec) controlSpec;
				if ((gallerySpec.getImageWidth() + gallerySpec.getImageHeight()) == 0) {
					ImageFetcher.getInstance();
				} else {
					ImageFetcher.getInstance(getApplicationContext(),
							gallerySpec.getImageWidth(),
							gallerySpec.getImageHeight());
				}
				ImageFetcher.getInstance()
						.setReadOnly(gallerySpec.isReadOnly());
				ImageFetcher.getInstance().setMaxZoom(gallerySpec.getMaxZoom());
				ImageFetcher.getInstance().setMinZoom(gallerySpec.getMinZoom());
				final ImageCache.ImageCacheParams cacheParams = new ImageCache.ImageCacheParams(
						getApplicationContext(),
						GenericFragment.IMAGE_CACHE_DIR);
				cacheParams.setMemCacheSizePercent(0.25f);
				ImageFetcher.getInstance().addImageCache(getFragmentManager(),
						cacheParams);
				ImageFetcher.getInstance().setImageFadeIn(true);
				this.mListRefreshView.add(baseView);
			} else if (controlType.equals(ControlType.SWITCH)) {
				baseView = new ApLinkSwitch(this, controlSpec, view, bundle);
			} else if (controlType.equals(ControlType.GOOGLE_MAPS)) {
				baseView = new ApLinkGoogleMaps(this, controlSpec, view, bundle);
			} else if (controlType.equals(ControlType.AUTOCOMPLETE_TEXTVIEW)) {
				baseView = new ApLinkAutoCompleteTextView(this, controlSpec,
						view, bundle);
			} else if (controlType.equals(ControlType.FLOATING_AUTOCOMPLETE)) {
				baseView = new ApLinkFloatingAutoComplete(this, controlSpec,
						view, bundle);
			} else if (controlType.equals(ControlType.TOGGLE_BUTTON)) {
				baseView = new ApLinkToggleButton(this, controlSpec, view,
						bundle);
			}
			
			if (baseView != null) {
				if (controlSpec.getMappingName() != null) {
					this.mBaseViewByMappingName.put(
							controlSpec.getMappingName(), baseView);
				}
				this.mBaseViewByID.put(controlSpec.getIDName(), baseView);
				mNotTranslate.put(controlId, baseView);
				if (baseView.hasValueList()) {
					this.mValueListViews.add(baseView);
				}
				
				if (controlSpec.getValidationGroup() != null) {
					final String validationGroup = controlSpec
							.getValidationGroup();
					if (this.mValidationGroups.containsKey(validationGroup)) {
						this.mValidationGroups.get(
								controlSpec.getValidationGroup()).add(
								controlSpec);
					} else {
						final ArrayList<ControlSpec> arrayList = new ArrayList<ControlSpec>();
						arrayList.add(controlSpec);
						this.mValidationGroups.put(validationGroup, arrayList);
					}
				}
				baseView.clear();
				if (mOnRotateListener != null && bundle != null) {
					mOnRotateListener.onRotateCompleted(bundle);
				}
				baseView.initView();
			}
		}
	}
	
	public void nextPage() {
		if (this.mTargetClass != null) {
			try {
				this.startActivity(this.mTargetClass.getTargetClass(),
						this.mTargetClass.getPassingValue());
			} catch (NameNotFoundException | ClassNotFoundException e) {
				e.printStackTrace();
			}
		}
	}
	
	public void notifyDataSetChanged() {
		if (this.isAlowRefresh) {
			for (final ApLinkBaseView baseView : this.mListRefreshView) {
				baseView.notifyDataSetChanged();
			}
			this.isAlowRefresh = false;
			mAlowRefresh = Activity.RESULT_OK;
		}
	}
	
	@Override
	public final void onActivityCreated(final Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		initData();
		mappingControl(savedInstanceState);
		setHasOptionsMenu(getFormDesign().hasOptionMenu());
	}
	
	@Override
	public void onActivityResult(final int requestCode, final int resultCode,
			final Intent data) {
		if (requestCode == REFRESH_DATA) {
			if (resultCode == Activity.RESULT_OK) {
				isAlowRefresh = true;
				notifyDataSetChanged();
			}
		}
		if (this.mActivityResultHandler != null) {
			this.mActivityResultHandler.onActivityResult(requestCode,
					resultCode, data);
		}
	}
	
	@Override
	public void onAnimationEnd(final Animation animation) {
		onSubmit(this.mSelectedControlSpec);
	}
	
	@Override
	public void onAnimationRepeat(final Animation animation) {
		
	}
	
	@Override
	public void onAnimationStart(final Animation animation) {
		
	}
	
	@Override
	public void onBackPressed() {
		finishActivity();
	}
	
	@Override
	public void onClick(final View v) {
		onSubmit((ControlSpec) v.getTag(R.id.CONTROL_SPEC));
	}
	
	@Override
	public void onCreate(final Bundle arg) {
		super.onCreate(arg);
		final Bundle arguments = getArguments();
		mFormDesign = FormDesignManager.getInstance().get(
				arguments.getInt(GenericFragment.FORM_DESIGN));
		mContentData = ContentDataManager.getInstance().get(
				arguments.getInt(GenericFragment.CONTENT_DATA));
		this.mGenericHandler = new ResponseHandler(this);
		this.mControlEventHandler = new EventHandler(this);
		this.mWebserviceHandler = new WebserviceHandler(this);
		this.mDatabaseHandler = new DataHandler(this);
		this.mBaseViewByMappingName = new GenericDictionary<String, ApLinkBaseView>();
		this.mBaseViewByID = new GenericDictionary<String, ApLinkBaseView>();
		this.mListRefreshView = new ArrayList<ApLinkBaseView>();
		this.mValueListViews = new ArrayList<ApLinkBaseView>();
		this.mValidationGroups = new GenericDictionary<String, ArrayList<ControlSpec>>();
		this.mApLinkProgressDialog = getFormDesign().getApLinkProgressDialog();
		if (this.mApLinkProgressDialog != null) {
			this.mApLinkProgressDialog.init(this);
		}
		this.mApLinkAlertDialog = getFormDesign().getAlertDialog();
		if (this.mApLinkAlertDialog != null) {
			this.mApLinkAlertDialog.init(this);
		}
		if (mFragmentStateListener != null) {
			mFragmentStateListener.onCreate(arg);
		}
		mNotTranslate = new GenericDictionary<>();
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		GenericDictionary<String, MenuItemSpec> contextMenu = (GenericDictionary<String, MenuItemSpec>) v
				.getTag(R.id.CONTEXT_MENU);
		for (int i = 0; i < contextMenu.size(); i++) {
			final MenuItemSpec menuItemSpec = contextMenu.valueAt(i);
			menu.add(Menu.NONE, contextMenu.keyAt(i),
					menuItemSpec.getOrderInCategory(), menuItemSpec.getTitle());
			menu.getItem(i).setIcon(menuItemSpec.getIconID());
		}
	}
	
	@Override
	public View onCreateView(final LayoutInflater inflater,
			final ViewGroup container, final Bundle savedInstanceState) {
		final int layoutID = getFormDesign().getLayoutID();
		if (layoutID != ResourcesHelper.RESOURCE_NOT_FOUND) {
			this.mView = inflater.inflate(layoutID, container, false);
			translate(mView, false);
		}
		return this.mView;
	}
	
	@Override
	public void onDestroy() {
		super.onDestroy();
		DatabaseHelper.getInstance().releaseDatabase();
		if (mFragmentStateListener != null) {
			mFragmentStateListener.onDestroy();
		}
	}
	
	protected void onFragmentCreated() {
		
	}
	
	public boolean onHandlePopupMenuItem(final MenuItemSpec menuItemSpec)
			throws NameNotFoundException, ClassNotFoundException {
		if ((menuItemSpec != null) && (menuItemSpec.getActionType() != null)) {
			this.mSelectedObject = menuItemSpec.getGenericObject();
			if (menuItemSpec.getTargetCollection() != null) {
				this.mTargetClass = menuItemSpec.getTargetCollection()
						.getTargetClass(this.mSelectedObject);
			}
			getEventHandler().sendAction(menuItemSpec.getActionType());
			return true;
		}
		return false;
	}
	
	@Override
	public boolean onNavigationItemSelected(final int itemPosition,
			final long itemId) {
		if (this.mActionBarSelectionListener != null) {
			String value = null;
			if (this.mIgnoreSelectionIndex != itemPosition) {
				value = this.mActionbarSelectionAdapter.getValue(itemPosition);
			}
			return this.mActionBarSelectionListener.onSelectionItemSelected(
					itemPosition, value, getFormDesign().getActionBar()
							.getSelectionSpec().getFilterField());
			
		}
		return false;
	}
	
	@Override
	public boolean onOptionsItemSelected(final MenuItem item) {
		final ActionBarSpec actionBarSpec = getFormDesign().getActionBar();
		final MenuItemSpec itemSpec = actionBarSpec.getOptionMenus().get(
				item.getItemId());
		if (item.getItemId() == android.R.id.home) {
			if (actionBarSpec.isDisplayHomeAsUp()) {
				finishActivity();
			}
		} else {
			if (getFormDesign().hasOptionMenu()) {
				onSubmit(itemSpec);
			}
		}
		return super.onOptionsItemSelected(item);
	}
	
	@Override
	public void onPause() {
		super.onPause();
		if (mFragmentStateListener != null) {
			mFragmentStateListener.onPause();
		}
	}
	
	protected void onPrepareFragment() {
		
	}
	
	@Override
	public void onPrepareOptionsMenu(final Menu menu) {
		final ActionBarSpec actionBarSpec = getFormDesign().getActionBar();
		if (actionBarSpec != null) {
			menu.clear();
			if (actionBarSpec.getSearchViewSpec() != null) {
				this.mSearchViewSpec = actionBarSpec.getSearchViewSpec();
				menu.add(Menu.NONE, this.mSearchViewSpec.getID().hashCode(),
						Menu.NONE, this.mSearchViewSpec.getTitle());
				menu.getItem(0).setShowAsAction(
						MenuItem.SHOW_AS_ACTION_ALWAYS
								| MenuItem.SHOW_AS_ACTION_WITH_TEXT);
				mSearchView = new SearchView(getApplicationContext());
				SearchManager searchManager = (SearchManager) getActivity()
						.getSystemService(Context.SEARCH_SERVICE);
				SearchableInfo searchableInfo = searchManager
						.getSearchableInfo(getActivity().getComponentName());
				mSearchView.setSearchableInfo(searchableInfo);
				// mSearchView.setQueryHint(mSearchViewSpec.getTitle());
				mSearchView.setOnCloseListener(new OnCloseListener() {
					
					@Override
					public boolean onClose() {
						if (mOnSearchListener != null) {
							mOnSearchListener.onSearchClose();
						}
						return false;
					}
				});
				menu.getItem(0).setActionView(mSearchView);
			}
			GenericObject bindingObject = getMappingObject();
			for (int i = 0; i < actionBarSpec.getOptionMenus().size(); i++) {
				final MenuItemSpec menuItemSpec = actionBarSpec
						.getOptionMenus().valueAt(i);
				menu.add(Menu.NONE, actionBarSpec.getOptionMenus().keyAt(i),
						menuItemSpec.getOrderInCategory(), Language
								.getInstance().get(menuItemSpec.getTitle()));
				menu.getItem(i).setIcon(menuItemSpec.getIconID());
				if (menuItemSpec.getShowAsActionType() != null) {
					menu.getItem(i).setShowAsAction(
							menuItemSpec.getShowAsActionType().getValue());
				}
				if (menuItemSpec.getEnableCondition() != null) {
					boolean isValid = menuItemSpec.getEnableCondition()
							.isValid(bindingObject);
					menu.getItem(i).setVisible(isValid);
				}
			}
		}
	}
	
	@Override
	public void onResume() {
		super.onResume();
		handleActionBar();
		if (mFragmentStateListener != null) {
			mFragmentStateListener.onResume();
		}
		languageChanged();
	}
	
	@Override
	public void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		if (mOnRotateListener != null) {
			mOnRotateListener.onSaveInstanceState(outState);
		}
	}
	
	@Override
	public void onStart() {
		super.onStart();
		if (mFragmentStateListener != null) {
			mFragmentStateListener.onStart();
		}
	}
	
	@Override
	public void onStop() {
		super.onStop();
		if (mFragmentStateListener != null) {
			mFragmentStateListener.onStop();
		}
	}
	
	public void onSubmit(ControlSpec controlSpec) {
		if (controlSpec != null) {
			this.mSelectedControlSpec = controlSpec;
		}
		if (mSelectedControlSpec.getActionType().equals(ActionType.CLOSE)) {
			finishActivity();
		} else {
			if (controlSpec != null
					&& hasRegisterAlertDialog(
							mSelectedControlSpec.getActionType(),
							DialogStatus.CONFIRM)) {
				getAlertDialog().show(mSelectedControlSpec.getActionType(),
						DialogStatus.CONFIRM);
			} else {
				if (TextUtils
						.isEmpty(mSelectedControlSpec.getValidationGroup())) {
					sendActionToEventHandler(mSelectedControlSpec,
							mSelectedControlSpec.getActionType());
				} else {
					new ControlValidationTask(this).executeOnExecutor(
							AsyncTask.THREAD_POOL_EXECUTOR,
							mSelectedControlSpec);
				}
			}
		}
	}
	
	private Intent passingValue(Class<?> target, GenericObject passingValue) {
		final Intent intent = new Intent(getApplicationContext(), target);
		if (!passingValue.isEmpty()) {
			Bundle bundle = new Bundle();
			bundle.putSparseParcelableArray(BundleConstant.SELECTED_OBJECT,
					passingValue);
			intent.putExtras(bundle);
		}
		return intent;
	}
	
	private void sendActionToEventHandler(ControlSpec controlSpec,
			final ActionType actionType) {
		mSelectedObject = this.getMappingObject();
		boolean isPassed = false;
		if (controlSpec instanceof MenuItemSpec) {
			isPassed = true;
		} else {
			isPassed = false;
			final EnableCondition enableCondition = controlSpec
					.getEnableCondition();
			if (enableCondition == null) {
				isPassed = true;
			} else {
				if (enableCondition.isValid(this.mSelectedObject)) {
					isPassed = true;
				} else {
					isPassed = false;
					getAlertDialog().show(actionType,
							DialogStatus.ENABLE_CONDITON_FAILED);
				}
			}
		}
		if (isPassed) {
			getTargetClassToNavigate(controlSpec);
			if (actionType != null) {
				if (mOnControlSubmit != null) {
					mOnControlSubmit.onControlSubmit(controlSpec);
				}
				getEventHandler().sendAction(actionType);
			}
		}
	}
	
	public void setActionBarSelectionListener(
			final OnActionBarSelectionListener actionBarSelectionListener) {
		this.mActionBarSelectionListener = actionBarSelectionListener;
	}
	
	protected StartActivityListener setActivityListener(
			final Class<?> startActivity) {
		final StartActivityListener activityListener = new StartActivityListener() {
			
			@Override
			public void startActivity() {
				GenericFragment.this.startActivity(startActivity,
						GenericFragment.this.mPassingValue);
			}
		};
		return activityListener;
	}
	
	public void setActivityResultListener(
			final OnActivityResultListener activityResultListener) {
		this.mActivityResultHandler = activityResultListener;
	}
	
	public void setAlowRefresh(final boolean isAlowRefresh) {
		this.isAlowRefresh = isAlowRefresh;
	}
	
	public void setFragmentStateListener(
			FragmentStateListener fragmentStateListener) {
		mFragmentStateListener = fragmentStateListener;
	}
	
	public void setHandleCustomViewListener(
			OnControlSubmitListener handleCustomViewListener) {
		mOnControlSubmit = handleCustomViewListener;
	}
	
	public void setOnActionbarButtonClickListener(
			final OnClickListener onActionbarButtonClickListener) {
		this.onActionbarButtonClickListener = onActionbarButtonClickListener;
	}
	
	public void setOnRotateListener(OnRotateListener onRotateListener) {
		mOnRotateListener = onRotateListener;
	}
	
	public void setOnSearchListener(final OnSearchListener onSearchListener) {
		this.mOnSearchListener = onSearchListener;
	}
	
	public void setSelectedObject(final GenericObject selectedObject) {
		this.mSelectedObject = selectedObject;
	}
	
	public void setTargetClass(final Target targetClass) {
		this.mTargetClass = targetClass;
	}
	
	public void setViewByID(final String id, final ApLinkBaseView baseView) {
		this.mBaseViewByID.put(id, baseView);
	}
	
	public void showInvalidValidationDialog(final ActionType actionType,
			final ValidationResult result) {
		if (!TextUtils.isEmpty(result.getMessage())) {
			if (getAlertDialog().has(actionType,
					DialogStatus.INVALID_VALIDATION)) {
				getAlertDialog().show(mSelectedControlSpec.getActionType(),
						DialogStatus.INVALID_VALIDATION, result.getMessage());
			}
		}
	}
	
	public void startActivity(Class<?> target, final PassingValue passingValue) {
		startActivity(target, passingValue, mSelectedObject);
	}
	
	public void startActivity(Class<?> target, final PassingValue passingValue,
			GenericObject selectedObject) {
		if (target == null) {
			return;
		}
		GenericObject genericObject = new GenericObject();
		if (!passingValue.isEmpty()) {
			for (Column column : passingValue) {
				if (!TextUtils.isEmpty(column.getDefaultValue())) {
					genericObject.put(column.getColumnName(),
							column.getDefaultValue());
				} else {
					String value = null;
					if (selectedObject != null) {
						value = selectedObject.getValue(column
								.getRealColumnName());
					}
					
					if (TextUtils.isEmpty(value)) {
						value = mInitValues
								.getValue(column.getRealColumnName());
					}
					if (!TextUtils.isEmpty(value)) {
						genericObject.put(column.getColumnName(), value);
					}
					
				}
			}
		}
		
		final Intent intent = passingValue(target, genericObject);
		startActivityForResult(intent, REFRESH_DATA);
		getActivity().overridePendingTransition(
				getFormDesign().getAnimationInID(),
				getFormDesign().getAnimationOutID());
	}
	
	private void translate(final View view, boolean isGetTag) {
		if (view instanceof ViewGroup) {
			for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {
				translate(((ViewGroup) view).getChildAt(i), isGetTag);
			}
		} else {
			if (view instanceof TextView
					&& view.getClass().equals(TextView.class)
					&& !mNotTranslate.containsKey(view.getId())) {
				if (isGetTag) {
					if (view.getTag(R.id.TEXT_ENGLISH) instanceof String) {
						((TextView) view).setText(Language.getInstance().get(
								((TextView) view).getTag(R.id.TEXT_ENGLISH)
										.toString()));
					}
				} else {
					view.setTag(R.id.TEXT_ENGLISH, ((TextView) view).getText());
					((TextView) view).setText(Language.getInstance().get(
							((TextView) view).getText().toString()));
				}
			}
		}
	}
	
	public boolean isHasLoadedData() {
		return hasLoadedData;
	}
	
	public void setHasLoadedData(boolean hasLoadedData) {
		this.hasLoadedData = hasLoadedData;
	}
}
